package com.cskaoyan.service;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.bo.AdminKeywordsListBO;
import com.cskaoyan.bean.bo.KeywordCreateBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.KeywordCreateVO;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author : gao
 * @since: 2022/10/13  15:48
 */
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {
    @Autowired
    MarketKeywordMapper keywordMapper;

    @Override
    public CommonData<MarketKeyword> listKeywords(AdminKeywordsListBO adminKeywordsListBO) {

        Integer page = adminKeywordsListBO.getPage();
        Integer limit = adminKeywordsListBO.getLimit();

        PageHelper.startPage(page, limit);
        List<MarketKeyword> keywordList = keywordMapper.selectList(adminKeywordsListBO);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(keywordList);

        CommonData<MarketKeyword> data = new CommonData<>();
        data.setTotal((int) pageInfo.getTotal());
        data.setPages(pageInfo.getPages());
        data.setLimit(limit);
        data.setPage(page);
        data.setList(keywordList);
        return data;


    }


    @Override
    public KeywordCreateVO createKeywords(KeywordCreateBO keywordCreateBO) {

        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keywordCreateBO.getKeyword());
        marketKeyword.setUrl(keywordCreateBO.getUrl());
        marketKeyword.setIsHot(keywordCreateBO.getIsHot());
        marketKeyword.setIsDefault(keywordCreateBO.getIsDefault());
        Date date = new Date();
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);

        keywordMapper.insertSelective(marketKeyword);
        KeywordCreateVO keywordCreateVO = keywordMapper.selectByPrimaryKey2(marketKeyword.getId());
        return keywordCreateVO;
    }

    @Override
    public MarketKeyword updateKeywords(MarketKeyword marketKeyword) {
        marketKeyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKeySelective(marketKeyword);
        MarketKeyword keyword = keywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return keyword;
    }

    @Override
    public void deleteKeyword(Integer id) {
        keywordMapper.deleteByPrimaryKey(id);
    }
}
